/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 横向滚动条视图
    /// </summary>
    public class FCHScrollBar : FCScrollBar {
        /// <summary>
        /// 创建视图
        /// </summary>
        public FCHScrollBar() {
        }

        /// <summary>
        /// 触摸回调事件
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="touchInfo"></param>
        /// <param name="invoke"></param>
        public override void callTouchEvent(String eventName, Object sender, FCTouchInfo touchInfo, Object invoke) {
            base.callTouchEvent(eventName, sender, touchInfo.clone(), invoke);
            if (sender == getBackButton()) {
                if (eventName == "ontouchdown") {
                    onBackButtonTouchDown(touchInfo.clone());
                } else if (eventName == "ontouchup") {
                    onBackButtonTouchUp(touchInfo.clone());
                }
            }
        }

        /// <summary>
        /// 销毁方法
        /// </summary>
        public override void delete() {
            if (!isDeleted()) {
                FCButton backButton = getBackButton();
                if (backButton != null) {
                    backButton.removeEvent(this, "ontouchdown");
                    backButton.removeEvent(this, "ontouchup");
                }
            }
            base.delete();
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "HScrollBar";
        }

        /// <summary>
        /// 拖动滚动方法
        /// </summary>
        public override void onDragScroll() {
            bool floatRight = false;
            FCButton backButton = getBackButton();
            FCButton scrollButton = getScrollButton();
            int backButtonWidth = backButton.getWidth();
            int contentSize = getContentSize();
            if (scrollButton.getRight() > backButtonWidth) {
                floatRight = true;
            }
            base.onDragScroll();
            if (floatRight) {
                setPos(contentSize);
            }
            else {
                setPos((int)((long)contentSize * (long)scrollButton.getLeft() / backButtonWidth));
            }
            onScrolled();
        }

        /// <summary>
        /// 添加视图方法
        /// </summary>
        public override void onLoad() {
            bool isAdd = false;
            FCButton backButton = getBackButton();
            if (backButton != null) {
                isAdd = true;
            }
            base.onLoad();
            if (!isAdd) {
                backButton = getBackButton();
                backButton.addEvent(this, "ontouchdown", null);
                backButton.addEvent(this, "ontouchup", null);
            }
        }

        /// <summary>
        /// 滚动条背景按钮触摸按下回调方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public void onBackButtonTouchDown(FCTouchInfo touchInfo) {
            FCButton scrollButton = getScrollButton();
            FCPoint mp = touchInfo.m_firstPoint;
            if (mp.x < scrollButton.getLeft()) {
                pageReduce();
                setIsReducing(true);
            }
            else if (mp.x > scrollButton.getRight()) {
                pageAdd();
                setIsAdding(true);
            }
        }

        /// <summary>
        /// 滚动条背景按钮触摸抬起方法
        /// </summary>
        /// <param name="touchInfo">触摸信息</param>
        public void onBackButtonTouchUp(FCTouchInfo touchInfo) {
            setIsAdding(false);
            setIsReducing(false);
        }

        /// <summary>
        /// 重新布局方法
        /// </summary>
        public override void update() {
            if (m_native != null && !m_native.m_lockUpdate)
            {
                FCButton addButton = getAddButton();
                FCButton backButton = getBackButton();
                FCButton reduceButton = getReduceButton();
                FCButton scrollButton = getScrollButton();
                int width = getWidth(), height = getHeight();
                int contentSize = getContentSize();
                //设置按钮的位置
                if (contentSize > 0 && addButton != null && backButton != null && reduceButton != null && scrollButton != null)
                {
                    int pos = getPos();
                    int pageSize = getPageSize();
                    if (pos > contentSize - pageSize)
                    {
                        pos = contentSize - pageSize;
                        m_pos = pos;
                    }
                    if (pos < 0)
                    {
                        pos = 0;
                        m_pos = pos;
                    }
                    int abWidth = addButton.isVisible() ? addButton.getWidth() : 0;
                    addButton.m_size = new FCSize(abWidth, height);
                    addButton.m_location = new FCPoint(width - abWidth, 0);
                    int rbWidth = reduceButton.isVisible() ? reduceButton.getWidth() : 0;
                    reduceButton.m_size = new FCSize(rbWidth, height);
                    reduceButton.m_location = new FCPoint(0, 0);
                    int backWidth = width - abWidth - rbWidth;
                    backButton.m_size = new FCSize(backWidth, height);
                    backButton.m_location = new FCPoint(rbWidth, 0);
                    //获取滚动条宽度和坐标
                    int scrollWidth = backWidth * pageSize / contentSize;
                    int scrollPos = backWidth * pos / contentSize;
                    if (scrollWidth < 10)
                    {
                        scrollWidth = 10;
                        if (scrollPos + scrollWidth > backWidth)
                        {
                            scrollPos = backWidth - scrollWidth;
                        }
                    }

                    scrollButton.m_size = new FCSize(scrollWidth, height);
                    scrollButton.m_location = new FCPoint(scrollPos, 0);
                }
            }
        }
    }
}
